<?php
/**
 * Main server.
 */
require_once("../util/SimpleMap.php");
require_once("../util/XConstants.php");

function process() {
   $op = isset($_POST["op"]) ? $_POST["op"] : "";

   if($op == XConstants::$OP_GET_SNACKS) {
      processGetSnacks($_POST);
   }
   else if($op == XConstants::$OP_UPDATE_SNACK) {
      processUpdateSnacks($_POST);
   }
   else if($op == XConstants::$OP_GET_NEWS) {
      processGetNews();
   }
   else {
      print_r($_POST);
   }
}

//function processAjaxRequest() {}
//
//function processGetRequest() {}
//
//function processPostRequest() {}
//
//function processNormalRequest() {}

function processGetSnacks() {
   require_once("../es/Snacks.php");

   $snacks = new Snacks();
   $snacks->queryInfo();
   $rsps = new SimpleMap();
   $rsps->put(XConstants::$RESPONSE_CODE, XConstants::$SUCCESS);
   $values = $snacks->getSnacksMap()->values();

   for($i = 0; $i < count($values); $i++) {
      $rsps->put("snack" . $i, $values[$i]->toString());
   }

   echo $rsps->writeBody();
}

function processGetNews() {
   require_once("../es/News_R.php");

   $news = new News_R();
   $news->queryInfo();
   $rsps = new SimpleMap();
   $rsps->put(XConstants::$RESPONSE_CODE, XConstants::$SUCCESS);
   $rsps->put("news", $news->toString());
   echo $rsps->writeBody();
}

process();
?>